import unittest
import sys, os
from os.path import join, dirname
location = join(dirname(sys.argv[0]), '..')

sys.path.insert(0, location)

import port_query


class PortQueryTest(unittest.TestCase):
    def testAll(self):
        self.assertEquals(port_query.PortQuery.all.str(), "all")
        
    def testCurrent(self):
        self.assertEquals(port_query.PortQuery.current.str(), "current")
        
    def testOutdated(self):
        self.assertEquals(port_query.PortQuery.outdated.str(), "outdated")
        
    def testInactive(self):
        self.assertEquals(port_query.PortQuery.inactive.str(), "inactive")
        
    def testActive(self):
        self.assertEquals(port_query.PortQuery.active.str(), "active")
        
    def testInstalled(self):
        self.assertEquals(port_query.PortQuery.installed.str(), "installed")
        
    def testUninstalled(self):
        self.assertEquals(port_query.PortQuery.uninstalled.str(), "uninstalled")
        
    def testSimpleConstructor(self):
        for word in ["foo", "boo", "doo", "dookoo"]:
            self.assertEquals(port_query.PortQuery(word).str(), word)
    
    def testSimpleAnd(self):
        w1 = port_query.PortQuery("foo")
        w2 = port_query.PortQuery("boo")
        w12 = w1 & w2
        self.assertEquals(w12.str(), "foo and boo")
    
    def testSimpleOr(self):
        w1 = port_query.PortQuery("foo")
        w2 = port_query.PortQuery("boo")
        w12 = w1 | w2
        self.assertEquals(w12.str(), "foo or boo")  
        
    def testAssocAnd(self):
        w1 = port_query.PortQuery("foo")
        w2 = port_query.PortQuery("boo")
        w3 = port_query.PortQuery("doo")
        w12_3 = (w1 & w2) & w3
        w1_23 = w1 & (w2 & w3)
        self.assertEquals(w12_3.str(), "(foo and boo) and doo")
        self.assertEquals(w1_23.str(), "foo and (boo and doo)")
        
    def testAssocOr(self):
        w1 = port_query.PortQuery("foo")
        w2 = port_query.PortQuery("boo")
        w3 = port_query.PortQuery("doo")
        w12_3 = (w1 | w2) | w3
        w1_23 = w1 | (w2 | w3)
        self.assertEquals(w12_3.str(), "(foo or boo) or doo")
        self.assertEquals(w1_23.str(), "foo or (boo or doo)")
        
        
    def testAssocAndOr(self):                                  
        w1 = port_query.PortQuery("foo")                     
        w2 = port_query.PortQuery("boo")                     
        w3 = port_query.PortQuery("doo")                     
        w12_3 = (w1 | w2) & w3                               
        w1_23 = w1 | (w2 & w3)
        w12_3b = (w1 & w2) | w3                               
        w1_23b = w1 & (w2 | w3)                               
        self.assertEquals(w12_3.str(), "(foo or boo) and doo")
        self.assertEquals(w1_23.str(), "foo or (boo and doo)")
        self.assertEquals(w12_3b.str(), "(foo and boo) or doo")
        self.assertEquals(w1_23b.str(), "foo and (boo or doo)")
		
    def testNot(self):
	w1 = port_query.PortQuery("foo")
	nw1 = ~w1
	self.assertEquals(nw1.str(), "not foo")
            
if __name__ == '__main__':
    unittest.main()
